<template>
  <div class="login">
    <!-- 如果给login这个盒子注册点击消失，会让 login-box 里面的每个元素被点击的时候都会冒泡，就会触发login的点击 -->
    <!-- 把事件注册在 login-box 的兄弟上面，让这个兄弟，和login 一样大 -->
    <div class="mask" @click="$store.commit('modifyShowLogin', false)"></div>
    <div class="login-box">
      <div class="close">
        <img src="../../assets/images/login/userArrow.png" alt="" />
      </div>
      <ul class="login-type flex">
        <li :class="{ active: currentIndex === 0 }" @click="currentIndex = 0">
          手机号码登录
        </li>
        <li :class="{ active: currentIndex === 1 }" @click="weixinClickFn">
          微信扫码登录
        </li>
      </ul>
      <div v-show="currentIndex === 0" class="phone-login">
        <div class="form-item">
          <input type="text" placeholder="输入手机号" v-model="phone" />
        </div>
        <div class="form-item">
          <slide-verify
            class="slide-box"
            :l="60"
            :r="10"
            :w="395"
            :h="135"
            :slider-text="msg"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
        </div>
        <div class="form-item">
          <input type="text" placeholder="输入难码" v-model.trim="code" />
          <span class="btn" @click="getVeriryCode"
            >获取验证码<span v-show="time !== 60">({{ time }})</span></span
          >
        </div>
        <div class="form-item">
          <div class="btn big" @click="phoneLogin">登录</div>
        </div>
      </div>
      <div v-show="currentIndex === 1" class="wx-login">
        <div id="weixin"></div>
      </div>
    </div>
  </div>
</template>

 
<script>
import { validateTelephone } from "../../utils";
import { toast } from "../../utils/toast.js";
import { getVerifyApi, phoneLoginApi } from "../../http/api";
export default {
  data() {
    return {
      currentIndex: 0,
      msg: "向右滑动",
      time: 60,
      phone: "",
      code: "",
      // 是否已经滑动验证成功
      isSlideVerfiy: false,
    };
  },
  methods: {
    // 点击微信登录
    weixinClickFn() {
      this.currentIndex = 1
      const _this = this
      // 就是使用 我们在 html里面引入 js，里面给我们提供下面这个 构造函数，new这个构造函数，就给我们提供一个可以使用微信扫一下图片
      new WxLogin({
        // id 指定一个id，把生成二维码，插入里面
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // appid是公司去申请的，在微信开发者的后台申请
        scope: "snsapi_login",
        // 扫码成功后重定向的接口 - 这个部分是后台的服务器的地址
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          // 微信父传递回来的数据，会以浏览器的地址栏的形式给你，你需要给它一个跳转的地址
          window.btoa(process.env.VUE_APP_STATE_URL + '/#' + _this.$route.path) 
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLCAuaW1wb3dlckJveCAuaW5mb3sNCiAgICBkaXNwbGF5OiBub25lOw0KfQ0KDQouaW1wb3dlckJveCAucXJjb2Rlew0KICAgIG1hcmdpbi10b3A6IDIwcHg7DQp9DQoNCg==",
      });
    },
    // 手机登录
    async phoneLogin() {
      // 先要验证手机号
      if (!validateTelephone(this.phone))
        return toast("请先填写手机号", "dander");
      // 验证是否已经拖动验证成功
      if (!this.isSlideVerfiy) return toast("请先拖动验证", "dander");
      // 难验证码写了没有
      if (this.code === "") return toast("请输入验证码", "dander");
      // 发请求
      const res = await phoneLoginApi({ verifyCode: this.code, phone: this.phone });
      // console.log(res);
      if (res.code === 0) {
        // 登录成功后，把token存在localStorage
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 提示
        toast(res.message, "success");
        // 把login隐藏
        this.$store.commit("modifyShowLogin", false);
      }
    },
    getVeriryCode() {
      // 先要验证手机号
      if (!validateTelephone(this.phone))
        return toast("请先填写手机号", "dander");
      // 验证是否已经拖动验证成功
      if (!this.isSlideVerfiy) return toast("请先拖动验证", "dander");
      // 如果已经点过了，在倒计时了，也不发请求了
      if (this.time !== 60)
        return toast("验证码已经发送，请勿重复点击", "dander");

      // 验证码发送提示成功
      // toast("验证码已经发送成功，请稍等", "success");

      // 修改时间，让倒计时显示
      this.time--;
      const timer = setInterval(() => {
        this.time--;
        if (this.time == -1) {
          this.time = 60;
          clearInterval(timer);
        }
      }, 1000);
      //   发请求
      //  ip+端口的那个接口有问题，先用下面的 域名作为接口使用
      getVerifyApi(this.phone).then((res) => {
        // console.log(res);
        if (res.code === 0) {
          // 发送短信成功了
          toast("验证码已经发送成功，请稍等", "success");
        }
      });
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "验证成功, 耗时 " + ms + "s";
      this.isSlideVerfiy = true;
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
  },
};
</script>
 
<style lang = "less" scoped>
.login {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .login-box {
    box-sizing: border-box;
    width: 555px;
    height: 423px;
    background-image: url("../../assets/images/login/loginBg.png");
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px 80px;
    .close {
      position: absolute;
      top: 20px;
      right: 60px;
      cursor: pointer;
    }
    .login-type {
      justify-content: center;
      li {
        padding: 0 10px;
        cursor: pointer;
        color: #cccc;
        &.active {
          color: #333;
        }
        &:nth-child(2) {
          border-left: 1px solid #ccc;
        }
      }
    }
    .phone-login {
      padding-top: 20px;
      .form-item {
        margin-bottom: 20px;
        display: flex;
        input {
          box-sizing: border-box;
          padding: 0 10px;
          height: 50px;
          border: 1px solid #ccc;
          flex: 1;
        }
        .btn {
          cursor: pointer;
          color: #fff;
          width: 120px;
          height: 50px;
          text-align: center;
          line-height: 50px;
          background-color: #0a328e;
          margin-left: 10px;
          &.big {
            width: 100%;
            margin: 0;
          }
        }
      }
    }
    .wx-login {
      #weixin {
        display: flex;
        justify-content: center;
      }
    }
  }
}

/* 在vue里面，如果想从父组件里面修改子组件的样式，需要在前面加上 /deep/ */
/deep/ .slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -140px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -140px;
    display: none;
  }
  .container-success {
    .slide-verify-slider-text {
      display: block;
      position: relative;
      z-index: 99;
    }
  }

  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>